/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.app.ui.view;

import cn.hutool.core.io.FileUtil;
import com.formdev.flatlaf.FlatClientProperties;
import com.google.common.eventbus.EventBus;
import m2tk.assistant.app.ui.util.ListModelOutputStream;
import m2tk.assistant.app.util.TextListLogAppender;
import m2tk.assistant.common.InfoView;
import m2tk.assistant.common.M2TKDatabase;
import m2tk.assistant.common.ui.RoundPanel;
import net.miginfocom.swing.MigLayout;
import org.kordamp.ikonli.fluentui.FluentUiRegularAL;
import org.kordamp.ikonli.swing.FontIcon;
import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import static m2tk.assistant.common.ui.UIConstants.*;

@Extension
public class LogsView extends RoundPanel implements InfoView
{
    private static final Logger log = LoggerFactory.getLogger(LogsView.class);

    private DefaultListModel<String> model;
    private EventBus bus;

    private final String viewTitle = "日志";
    private final Icon viewIcon = FontIcon.of(FluentUiRegularAL.BOOK_PULSE_24, ICON_SIZE_L, LIGHT_BLUE);

    public LogsView()
    {
        initUI();
    }

    private void initUI()
    {
        model = new DefaultListModel<>();
        JList<String> list = createList();
        list.setFixedCellHeight(25);
        list.setModel(model);
        list.setDragEnabled(false);
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        TextListLogAppender.setStaticOutputStream(new ListModelOutputStream(model));

        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.putClientProperty(FlatClientProperties.STYLE,
                                     """
                                     borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                     """);

        setLayout(new MigLayout("insets 10", "[][grow][][]", "[25!][pref!][][grow]"));
        add(new JLabel(viewTitle,
                       FontIcon.of(FluentUiRegularAL.BOOK_PULSE_24, ICON_SIZE_S, LIGHT_BLUE),
                       SwingConstants.TRAILING), "left");
        add(createControlButton(FontIcon.of(FluentUiRegularAL.DELETE_20, ICON_SIZE_S, LIGHT_BLUE),
                                e -> model.clear()),
            "skip");
        add(createControlButton(FontIcon.of(FluentUiRegularAL.DOCUMENT_COPY_20, ICON_SIZE_S, LIGHT_BLUE),
                                e ->
                                {
                                    try
                                    {
                                        File logsDir = FileUtil.file(FileUtil.getUserHomeDir(), "m2tk", "logs");
                                        Desktop.getDesktop().open(logsDir);
                                    } catch (IOException ex)
                                    {
                                        log.warn("打开日志目录异常：{}", ex.getMessage());
                                    }
                                }),
            "wrap");
        add(separator(), "span 4, grow, wrap");
        add(scrollPane, "span 4 2, grow");
    }

    @Override
    public void setupDataSource(EventBus bus, M2TKDatabase database)
    {
        this.bus = bus;
        bus.register(this);
    }

    @Override
    public JComponent getViewComponent()
    {
        return this;
    }

    @Override
    public String getViewTitle()
    {
        return viewTitle;
    }

    @Override
    public Icon getViewIcon()
    {
        return viewIcon;
    }
}
